<?php
class CategoryAction extends BaseAction{
	/**
	 * To display all Categories
	 */
	public function index(){
		$Category = M('Category');
		$categories = $Category->select();
		$this->assign('categories',$categories);
		$this->display();
	}
	/**
	 * To add a new category
	 */
	public function addcategory(){
		$Category = M('Category');
		$Category->create();
		$isadd = $Category->add();
		if($isadd){
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->success('Succeed to add a new category!');
		}else {
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->error('Failed to add a new category!');
		}
	}
	/**
	 * To update a category
	 */
	public function updatecategory(){
		$Category = M('Category');
		$Category -> create();
		$isupdate = $Category->where('category_id='.$_POST['category_id'])->save();
		if($isupdate){
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->success('Succeed to update a new category!');
		}else {
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->error('Failed to update a new category!');
		}
	}
	/**
	 * To delete a category
	 */
	public function deletecategory(){
		$Category = M('Category');
		$Channel = M('Channel');
		$isnull = $Channel->where('channel_category='.$_GET['id'])->select();
		if(count($isnull) == 0)
			$isdelete = $Category->where('category_id='.$_GET['id'])->delete();
		else 	
			$isdelete = FALSE;
		if($isdelete){
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->success('Succeed to delete a new category!');
		}else {
			$this->assign('waitSecond',5);
			$this->assign('jumpUrl','index');
			$this->error('Failed to delete a new category! You should delete all channels of this category!');
		}
	}
	/**
	 * Show and Edit the info of a category
	 */
	public function editcategory(){
		$Category = M('Category');
		$acategory = $Category->where('category_id='.$_GET['id'])->find();
		$categories = $Category->select();
		$this->assign('categories',$categories);
		$this->assign('category',$acategory);
		$this->display();
	}
	/**
	 * Show the page of new category
	 * Enter description here ...
	 */
	public function newcategory(){
		$Category = M('Category');
		$categories = $Category->select();
		$this->assign('categories',$categories);
		$this->display();
	}
}
?>